package drp.snowbell.module;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import weibo.snowbell.cache.CacheSystem;
import weibo.snowbell.cache.ICache;
import weibo.snowbell.cfg.EntityConfig;
import weibo.snowbell.cfg.PartParameter;
import weibo.snowbell.cfg.PropertyConfig;
import weibo.snowbell.entity.IEntity;
import weibo.snowbell.entity.Property;
import weibo.snowbell.entity.Relation;
import weibo.snowbell.util.StrHelper;

import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager;

public class EntityInfo {
	private ICache<String, Property> propertyMap = CacheSystem.getCache(EntityInfo.class.getName() + "#property");
	private ICache<String, Relation> relationMap = CacheSystem.getCache(EntityInfo.class.getName() + "#relation");
	private ICache<String, Property> allPropertyMap = CacheSystem.getCache(EntityInfo.class.getName() + "#allproperty");

	private ICache<String, PartInfo> partInfoMap;

	private ICache<String, Property> fullTextPropertyMap = CacheSystem.getCache(EntityInfo.class.getName() + "#fullTextPropertyMap");

	private String moduleName;
	private String entityName;
	private String tableName;
	private boolean isGenerated;
	private boolean hasFulltext;

	private Class<? extends IEntity> entityClass;

	public EntityInfo(Class<? extends IEntity> entityClass, String moduleName, EntityConfig config,) {
		this.moduleName = moduleName;
		this.entityName = config.getName();
		this.tableName = config.getTableName();

		this.isGenerated = config.getIsGenerated();

		this.entityClass = entityClass;
		String tableName = config.getTableName();
		if (StrHelper.isNullOrEmpty(tableName)) {
			// 表名称默认为实体名称
			this.tableName = entityName;
		} else {
			this.tableName = tableName;
		}

 		Collection<Property> propertyList = changProertyConfig(entityClass, config.getName(), config.getProperties());
		for (Property prop : propertyList) {
			propertyMap.put(prop.getName(), prop);
			if (prop.getFullTextConfig().getEnable()) {
				fullTextPropertyMap.put(prop.getName(), prop);
			}
		}
		
		this.hasFulltext = fullTextPropertyMap.size()>0;
		
		//初始化部件
		for (PartParameter partParam: config.getParts()){
			String partName = partParam.getPartName();
			
		}

		// 初始化allProperty
 		Collection<PartParameter> partParams = config.getParts();
		for (PartParameter param : partParams) {
			String strParam = param.getPartName();
			Collection<Property> props = skillDna.getPropertyList();
			allProperty.addAll(props);
		}
 
		this.storageClassName = config.getStorageClass Name();

		this.beanClass = clazz;
		ValidationManager.register(beanName, config.getValidationClass());

		this.propId = getProperty(Property.PROPERTY_ID);
		this.propDeleted = getProperty(Property.PROPERTY_DELETED);
		this.propName = getProperty(Property.PROPERTY_NAME);
		this.propStorageVersion = getProperty(Property.PROPERTY_STORAGE_VERSION);
		String fullTextAnalyzer = config.getFullTextAnalyzer();
		if (StrHelper.isNullOrEmpty(fullTextAnalyzer)) {
			fullTextAnalyzer = Config.get().getFullTextDefaultAnalyzer();
		}
		this.fullTextAnalyzer = fullTextAnalyzer;
		// 注册配置数据
		Map<String, ConfigItemDef> configTemp = ModuleConfigManager.toConfigItemDef(config.getConfigDefinitions());
		for (ConfigItemDef cid : configTemp.values()) {
			ModuleConfigManager.register(beanName, cid);
		}
	}

	/**
	 * 把PropertyConfig列表转Property列表.用户构造函数
	 * 
	 * @param configList
	 * @return
	 */
	private static Collection<Property> changProertyConfig(Class<? extends IEntity> entityClass, String entityName,
			Collection<PropertyConfig> configList) {
		Collection<Property> ret = new ArrayList<Property>();
		for (PropertyConfig pc : configList) {
			Property prop = new Property(entityClass, pc, entityName, null);
			ret.add(prop);
		}
		return ret;
	}
}
